/*
* dma.h
*
*  Created on: 2021��5��31��
*      Author: Administrator
*/

#ifndef SRC_DMA_DMA_H_
#define SRC_DMA_DMA_H_

#include "xil_printf.h"
#include "xparameters.h"
#include "xstatus.h"

#include "xaxidma.h"
#include "../utility.h"

#define DMA_DEV_ID		        XPAR_AXIDMA_0_DEVICE_ID

#define RX_INTR_ID		        XPAR_INTC_0_AXIDMA_0_S2MM_INTROUT_VEC_ID
#define TX_INTR_ID		        XPAR_INTC_0_AXIDMA_0_MM2S_INTROUT_VEC_ID
#define RESET_TIMEOUT_COUNTER	10000

#define MAX_PKT_LEN		1500
#define MAX_RAPIDIO_LEN 512

typedef enum {
    NWRITE = 0,
    DOORBELL
}RapidIO_type;

typedef enum {
    STATE_IDLE,
    STATE_HEADER_OK,
    STATE_DATA_OK_WAIT_DOORBELL
}rapidio_rcv_state;

typedef enum{
    FRAME_COMPLETE = 0,
    FRAME_OK,
    FRAME_HEADER_ERROR,
    FRAME_DATA_ERROR,
    FRAME_DOORBELL_ERROR
}frame_status;

typedef struct{
	u16 id;
	u8 dir;
	u8 data_type;
	u16 msg_size;
	u8 tx_seq;
	u8 transfer_data_type;
}frame_header;

typedef struct{
    u8 TxDone;
    u8 RxDone;
    u8 Error;
    u8 *RxBufferPtr;    // dma buffer MAX_RAPIDIO_LEN
    u8 *TxBufferPtr;    // dma buffer MAX_RAPIDIO_LEN
    u8 rx_offset;       // msg当前长度偏移, STATE_IDLE,须重置为0
    u8 tx_offset;       // ??
    u16 doorbell_size;
    int state;
    u8 *rxMsgBufferPtr; //移除RapidIO头 msg buffer MAX_PKT_LEN
    u8 *txMsgBufferPtr;
    frame_header frameHeader;
}DmaStatus;


/* extern interface */ 
int init_axidma(QueueHandle_t xEventQueue);
int dma_tx(u8 *TxPacket, int tx_length);
int process_rx(int rx_length);

void enable_tx_test(u32 tx_cnt, u32 clear_cnt);
int dmaTxRapidIOContinue(u8 rapidIoPacketIdx, u8 *sendEnFlagPtr, u8 *sendEnCntPtr, u32 *tx120packetCntPtr, State *xState);

//tmp
void copy_test_tx_data(u8 *testBufferPtr, u32 tx_length);

#endif /* SRC_DMA_DMA_H_ */
